package com.zhentao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.ApArticleMapper;
import com.zhentao.model.common.dtos.ApArticle;
import com.zhentao.model.common.dtos.ApArticleContent;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.ApArticleContentService;
import com.zhentao.service.FileStorageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【ap_article_content(APP已发布文章内容表)】的数据库操作Service实现
* @createDate 2024-06-01 10:28:32
*/
@Service
public class ApArticleContentServiceImpl extends ServiceImpl<ApArticleContentMapper, ApArticleContent>
    implements ApArticleContentService {

    @Resource
    private ApArticleContentMapper apArticleContentMapper;

    @Resource
    private FileStorageService fileStorageService;

    @Resource
    private ApArticleMapper apArticleMapper;

    @Resource
    private Configuration configuration;


    @Override
    public ResponseResult getArticleContentById(long articleId) throws IOException, TemplateException {
        //根据文件id获取文件内容
        //1.获取文章内容
        QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApArticleContent::getArticleId, articleId);
        ApArticleContent apArticleContent =
                this.apArticleContentMapper.selectOne(queryWrapper);
        if (apArticleContent.getContent() != null) {
            //2.文章内容通过freemarker生成html文件
            Template template = configuration.getTemplate("article.ftl");
            Map<String, Object> params = new HashMap<>();
            params.put("content", JSONArray.parseArray(apArticleContent.getContent()));

            StringWriter out=new StringWriter();
            template.process(params,out);

            //3.把html文件上传到minio中
            InputStream inputStream=new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8));
            String path=fileStorageService.uploadHtmlFile("article",
                    apArticleContent.getArticleId()+".html",
                    inputStream);
            //4.修改ap_article表的static_url字段
            ApArticle apArticle=new ApArticle();
            apArticle.setId(articleId);
            apArticle.setStaticUrl(path);
            this.apArticleMapper.updateById(apArticle);
            return ResponseResult.okResult(200,path);
        }

        return ResponseResult.errorResult(201,"修改失败");
    }
}




